package com.jfinal.club._admin.student;

import com.jfinal.aop.Inject;
import com.jfinal.club._admin.account.AccountAdminService;
import com.jfinal.club._admin.banji.BanjiAdminService;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Student;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

public class StudentAdminController extends BaseController{

	@Inject
	StudentAdminService srv;
	@Inject
	BanjiAdminService bsrv;
	@Inject
	AccountAdminService asrv;

	
	public void index() {
		Page<Student> studentPage = srv.paginate(getParaToInt("p", 1));
		setAttr("studentPage", studentPage);
		render("index.html");
	}
	
	public void add() {
		setAttr("banjiList", bsrv.getAll());
		setAttr("accountList", asrv.getAll());
		render("add_edit.html");
	}
	
	//@Before(MyProjectValidator.class)
	public void save() {
		Student student = getBean(Student.class);
		Ret ret = srv.save(0, student);
		renderJson(ret); 
	}
	
	
	public void edit() {
		keepPara("p");	// 保持住分页的页号，便于在 ajax 提交后跳转到当前数据所在的页
		setAttr("student", srv.edit(getParaToInt("id")));
		setAttr("banjiList", bsrv.getAll());
		setAttr("accountList", asrv.getAll());
		render("add_edit.html");
	}

	/**
	 * 提交修改
	 */
	//@Before(MyProjectValidator.class)
	public void update() {
		Student student = getBean(Student.class);
		Ret ret = srv.update(student);
		renderJson(ret);
	}
	
	//删除
	public void delete() {
		Ret ret = srv.delete(getParaToInt("id"));
		renderJson(ret);
	}
}
